﻿using System;
using Ewk.MachineLearning.Persistance.Domain;
using MongoDB.Bson.Serialization;

namespace Ewk.MachineLearning.Persistance.DataAccess
{
    class ClassMapper
    {
        public static void RegisterMaps()
        {
            RegisterMap<Conversation>();
            RegisterMap<Category>();
            RegisterMap<Document>();
            RegisterMap<Word>();
        }

        static void RegisterMap<T>() 
            where T : Entity
        {
            RegisterMap<T>(null);
        }

        static void RegisterMap<T>(Action<BsonClassMap<T>> action) 
            where T : Entity
        {
            if (BsonClassMap.IsClassMapRegistered(typeof(T))) return;

            BsonClassMap.RegisterClassMap<T>(
                classMap =>
                    {
                        classMap.AutoMap();
                        //classMap.MapIdProperty(arg => arg.Id);
                        classMap.SetIgnoreExtraElements(true);

                        if (action == null) return;

                        action(classMap);
                    });
        }
    }
}